<template>
  <div class="goods">
      <!-- 面包屑 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/goods' }">商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索框 -->
      <div class="search">
        <el-input placeholder="请输入内容" @keydown.13.native="handleSearch" v-model="searchVal" class="input-with-select searchInput">
          <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
        </el-input>
        <router-link to="/toadd"><el-button type="primary" plain @click="handleAddGoods">添加商品</el-button></router-link>
      </div>
      <!-- 表格 -->
      <el-table
        class="mt-15"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          type="index"
          label="编号"
          width="50">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="340">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="150">
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品状态"
          width="150">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="150">
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="150">
        </el-table-column>
        <el-table-column
          label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" plain icon="el-icon-edit" @click="handleEditGoods(scope.row)"></el-button>
              <el-button size="mini" type="danger" plain icon="el-icon-delete" @click="handleDelGoods(scope.row.goods_id)"></el-button>
              <el-button size="mini" type="warning" plain icon="el-icon-check"></el-button>
            </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum">
      </el-pagination>
  </div>
</template>

<script>
import { getGoodsList, deleteGoods } from '@/api'
export default {
  data () {
    return {
      searchVal: '',
      tableData: [],
      pagenum: 1,
      pagesize: 5,
      totalNum: 0

    }
  },
  created () {
    this.initTable()
  },
  methods: {
    // 初始化表格
    initTable () {
      getGoodsList(this.searchVal, this.pagenum, this.pagesize).then(res => {
        console.log(res)
        this.tableData = res.data.goods
        this.totalNum = res.data.total
      })
    },
    // 分页器函数
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initTable()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pagenum = val
      this.initTable()
    },
    // 搜索
    handleSearch () {
      this.initTable()
    },
    handleAddGoods () {

    },
    // 编辑商品
    handleEditGoods (row) {
      this.$router.push({ path: `/toedit/${row.goods_id}` })
    },
    // 删除商品
    handleDelGoods (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods(id).then(res => {
          // console.log(res)
          if (res.meta.status === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initTable()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style scoped>
  .searchInput{
    width: 300px;
    margin-right:5px;
  }
</style>
